package com.css.sword.knowing.toolkit.file;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 作者 E-mail:caihx@css.com.cn
 * @date 创建时间：2017年3月21日 下午3:55:42
 * @version 1.0
 */
public class CssFileUtil {

	/**
	 * 将文件读取到流
	 * 
	 * @param fpath
	 *            文件路径
	 * @return byte数组
	 * @throws IOException
	 */
	public static byte[] readStream(String fpath) throws IOException {

		File file = new File(fpath);
		if (fpath == null || fpath.equals("")) {
			throw new NullPointerException("无效的文件路径");
		}
		long len = file.length();
		byte[] bytes = new byte[(int) len];

		BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file));
		int r = bufferedInputStream.read(bytes);
		if (r != len)
			throw new IOException("读取文件不正确");
		bufferedInputStream.close();

		return bytes;
	}

	/**
	 * 将流写入到文件
	 * 
	 * @param fpath
	 *            文件路径
	 * @param bytes
	 *            文件内容
	 * @throws IOException
	 */
	public static void writeStream(String fpath, byte[] bytes) throws IOException {
		File f = new File(fpath);

		FileOutputStream fop = new FileOutputStream(f);
		// OutputStreamWriter writer = new OutputStreamWriter(fop, "UTF-8");
		// writer.append(context);
		// writer.close();

		fop.write(bytes);

		fop.close();
	}

	/**
	 * 将文件内容读取到字符串
	 * 
	 * @param fpath
	 *            文件路径
	 * @return 返回文件内容
	 * @throws IOException
	 */
	public static String readString(String fpath) throws IOException {
		String ret = "";

		File f = new File(fpath);

		FileInputStream fip = new FileInputStream(f);

		InputStreamReader reader = new InputStreamReader(fip, "UTF-8");

		StringBuffer sb = new StringBuffer();
		while (reader.ready()) {
			sb.append((char) reader.read());
		}
		reader.close();

		fip.close();

		ret = sb.toString();

		return ret;
	}

	/**
	 * 将字符串写入到文件中
	 * 
	 * @param fpath
	 *            文件路径
	 * @param context
	 *            字符串内容
	 * @param append
	 *            是否追加写入，true：追加，false：不追加
	 * @throws IOException
	 */
	public static void writeString(String fpath, String context, boolean append) throws IOException {

		File f = new File(fpath);

		f.createNewFile();

		FileWriter writer = new FileWriter(f, append);

		writer.write(context);
		writer.flush();
		writer.close();
	}

	/**
	 * 获取目录下的所有文件路径
	 * 
	 * @param fList
	 *            所有文件路径（及返回值）
	 * @param dir
	 *            待扫描的根目录
	 * @param suffix
	 *            文件后缀名称
	 */
	public static void getFileList(List<String> fList, File dir, String suffix) {
		if (dir == null)
			return;

		File[] fArray = dir.listFiles();

		for (int i = 0; i < fArray.length; i++) {
			if (fArray[i].isDirectory()) {
				getFileList(fList, fArray[i], suffix);
			} else {
				if (null != suffix && !suffix.isEmpty()) {
					if (fArray[i].getName().endsWith(suffix))
						fList.add(fArray[i].getPath());
				} else {
					fList.add(fArray[i].getPath());
				}
			}
		}
	}

	public static void main(String[] args) throws IOException {

		List<String> fList = new ArrayList<String>();

		getFileList(fList, new File("J:\\NLP\\test\\participle"), "");

		for (String fpath : fList) {
			System.out.println(fpath);
		}
	}
}
